function showPop() {
    var t = $(document).height();

    if ($('#pop-share').length <= 0) {
        $('body').append(' <div class="mod-pop" id="pop-share" onclick="hidePop(\'#pop-share\')"> <span class="text-share"></span> </div>');
    };
    var $obj=$('#pop-share');
    $($obj).height(t).fadeIn();
}
function hidePop(e) {
    $(e).fadeOut()
}
$.fn.tips=function(content){
    var tips=$(this);
    $(tips).append('<div class="ui-poptips ui-poptips-success am-animation-fade" > <div class="ui-poptips-cnt"><i></i>'+content+'</div> </div>');
    setTimeout(function(){$("div.ui-poptips").remove();},2000);
};
function getURLParameter(name) {
    return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null;
}
Number.prototype.toPrice = function () {
    return parseFloat(this.valueOf()).toFixed(2);
};
String.prototype.trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, '');
};
/**
 * 获取文档高度
 * @returns {number}
 */
function getDocumentTop() {
    return document.body ? document.body.scrollTop : document.documentElement.scrollTop;
}
/**
 * 获取滚动条高度
 * @returns {number}
 */
function getScrollHeight() {
    return document.body ? document.body.scrollHeight : document.documentElement.scrollHeight;
}
/**
 * 获取页面视口大小
 * @returns {Number}
 */
function getClientHeight(){
    var pageHeight = window.innerHeight;
    if(typeof pageHeight != 'number'){
        pageHeight = document.compatMode == 'CSS1Compat' ? document.documentElement.clientHeight : document.body.clientHeight
    }
    return pageHeight;
}
/**
 * 公共的返回上一页方法
 */
function handleBackClick(e){
    history.go(-1);
    if(e){
        e.stopPropagation();
        e.preventDefault();
    }
}

//---------------------------- config ----------------------------//
var IMG_PRE = 'http://120.77.86.178:41200';
var MARKET = { //专场ID
    today: 1,   //今日专场
    hot: 2    //爆款推荐
};
//商品列表页 搜索类型
var FROM = {
    index: 1,    //从首页 根据专场查询
    search: 2,   //从搜索页面
    confirm: 3,
    userInfo: 4
};
//返回接口调用options
var getAjaxOps = function(url, param){
    var type = 'POST';
    if(!param){
        type = 'GET';
        param = {};
    }
    console.log(url+':'+JSON.stringify(param));
    return {
        url: "http://120.77.86.178:41000"+url,
        // url: "http://127.0.0.1:8080"+url,
        data: param,
        // contentType: "application/json;charset=UTF-8",
        type: type,
        dataType: 'json'
    }
};
//用户信息
var USER = {
    userId: 0,
    session: ''
};
var getUser = function () {
    var user = localStorage.getItem('user');
    if (user) {
        user = JSON.parse(user);
        USER.userId = user.userId;
        USER.session = user.session;
        return USER;
    }else {
        return null;
    }
};
getUser();
var setUser = function(userId, session) {
    USER.userId = userId;
    USER.session = Math.round(session/86400000); //天数
    localStorage.setItem('user', JSON.stringify(USER));
};
var logout = function(){
    USER.userId = 0;
    USER.session = '';
    localStorage.removeItem('user');
};
var ifUser = function(){
    //需要登录信息的操作 判断是否有用户信息 没有则跳转到登录页
    var user = getUser();
    if(user === null){
        localStorage.setItem('pre_url', location.href);
        location.replace('login.html');
    } else{
        var newTime = new Date();
        if(Math.round(newTime.getTime/86400000) - parseInt(user.session) > 30){
            localStorage.setItem('pre_url', location.href);
            location.replace('login.html');
        }
    }
    return true;
};
//手机号、密码校验 Reg
var PHONE = /^1\d{10}$/;
var PWD = /(?!^[^A-z0-9]+$){6,30}$/;
//提示信息
var MSG = {
    PHONE_NOT_NULL: '手机号不能为空',
    PHONE_NOT_RIGHT: '请输入正确的手机号',
    PWD_NOT_NULL: '密码不能为空',
    PWD_NOT_RIGHT: '请输入密码',
    PWD_NOT_SAME: '两次密码不一样',
    CODE_NOT_NULL: '验证码不能为空',
    ADDRESS_RECEIVE_NAME: '请输入收货人姓名',
    ADDRESS_LOCATION: '请选择完整的省市信息',
    ADDRESS_DETAIL: '请输入详细地址'
};
//返回之前操作的地址
var PRE_URL = localStorage.getItem('pre_url') || 'index.html';
var clearPreUrl = function(){
    localStorage.removeItem('pre_url');
};

/**
 * 对话框
 */
var Dialog = React.createClass ({
    getInitialState: function(){
        return ({
            once: false //是否点击了确定按钮
        })
    },
    handleOffClick: function(e) {
        this.props.close();
        if(this.props.off){
            this.props.off();//传入的取消事件
        }
        e.stopPropagation();
        e.preventDefault();
    },
    handleOnClick: function(e) {
        // if(!this.state.once){
        //     this.setState({
        //         once: true
        //     });
            this.props.close();
            if (this.props.on){ //传入的确定 callback 方法
                this.props.on();
            }
        // }
        e.stopPropagation();
        e.preventDefault();
    },
    render: function(){
        return (
            <div className="am-dimmer am-active" data-am-dimmer="" style={this.props.hidden ? { 'display': 'none'} : { 'display': 'block'}}>
                <div className="am-modal am-modal-confirm am-modal-active" tabindex="-1" style={{'display': 'block', 'margin-top': '-50px'}}>
                    <div className="am-modal-dialog">
                        <div className="am-modal-bd font-16">{this.props.msg}</div>
                        <div className="am-modal-footer">
                            <span className="am-modal-btn" data-am-modal-cancel onClick={this.handleOffClick}>取消</span>
                            <span className="am-modal-btn" data-am-modal-confirm onClick={this.handleOnClick}>确定</span>
                        </div>
                    </div>
                </div>
            </div>
        )
    }
});
/**
 * 提示框
 */
var Message = React.createClass ({
    handleCloseClick: function(e){
        if(this.props.url){
            //跳转下一步页面
            location.href = this.props.url;
        }else if(this.props.close){
            //关闭提示框
            this.props.close();
        }
        e.stopPropagation();
        e.preventDefault();
    },
    render: function() {
        return (
            <div className="am-dimmer am-active" data-am-dimmer="" style={this.props.hidden ? { 'display': 'none'} : { 'display': 'block'}}>
                <div className="am-modal am-modal-confirm am-modal-active" tabindex="-1" style={{'display': 'block', 'margin-top': '-50px'}}>
                    <div className="am-modal-dialog">
                        <div className="am-modal-bd">{this.props.msg}</div>
                        <div className="am-modal-footer">
                            <span className="am-modal-btn" onClick={this.handleCloseClick}>确定</span>
                        </div>
                    </div>
                </div>
            </div>
        )
    }
});

/**
 * 头部
 */
var Header = React.createClass ({
    render: function() {
        return (
            <header data-am-widget="header" className="am-header am-header-default am-header-fixed">
                <div className="am-header-left am-header-nav">
                    <a onClick={handleBackClick}><i className="icon_8"></i></a>
                </div>
                <h1 className="am-header-title">{this.props.title}</h1>
                <div className="am-header-right am-header-nav am-dropdown" id="am-dropdown">
                    <a href="search.html"><i className="icon_270"></i></a>
                    {this.props.cart ? <a href="cart.html"><i className="ShoppingCart_4"></i></a> : ''}
                    <div className="am-dropdown-toggle icontopmore" id="am-dropdown-toggle"></div>
                    <ul className="am-dropdown-content">
                        <li className="padding-left-20"><a href="index.html" className="font-color-45 prelative">首页</a></li>
                        <li className="am-divider"></li>
                        <li className="padding-left-20"><a href="personal.html" className="font-color-45 prelative">个人中心</a></li>
                    </ul>
                </div>
            </header>
        )
    }
});

/**
 * 页脚
 */
var Footer = React.createClass({
    handleRedirectClick: function() {
        $.ajax(
            getAjaxOps('/config/getQQ', {})
        ).done(function (res) {
            if(res.resultCode == 0){
                window.frontshoes.chat(res.data.toString());
            }
        }.bind(this));
        // location.href = 'mqqwpa://im/chat?chat_type=wpa&uin=348757652&version=1&src_type=web&web_src=bjhuli.com';
    },
    render: function(){
        var imgList = ['home', 'ShoppingCart', 'my'];
        switch (this.props.on){
            case 'index': imgList[0] = 'home_on';break;
            case 'cart': imgList[1] = 'ShoppingCart_on';break;
            case 'personal': imgList[2] = 'my_on';break;
        }
        return (
            <footer data-am-widget="navbar" className="am-navbar am-cf am-navbar-default">
                <ul className="am-navbar-nav am-cf am-avg-sm-4">
                    <li><a href="index.html" className={this.props.on=='index' ? 'selected' : ''}><img src={"../assets/images/icons/"+imgList[0]+".png"} alt="首页" /><span className="am-navbar-label">首页</span></a></li>
                    <li><a href="#" onClick={this.handleRedirectClick}><img src="../assets/images/icons/service.png" alt="客服" /><span className="am-navbar-label">客服</span></a></li>
                    <li><a href="cart.html" className={this.props.on=='cart' ? 'selected' : ''}><img src={"../assets/images/icons/"+imgList[1]+".png"} alt="进货车" /><span className="am-navbar-label">进货车</span></a></li>
                    <li><a href="personal.html" className={this.props.on=='personal' ? 'selected' : ''}><img src={"../assets/images/icons/"+imgList[2]+".png"} alt="我的" /><span className="am-navbar-label">我的</span></a></li>
                </ul>
            </footer>
        )
    }
});


$(function () {
    $('#am-dropdown').dropdown({justify: '#am-dropdown-toggle'});
});